package cn.cttic.app.api.dubbo;

import cn.cttic.base.api.domain.BaseUser;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.cttic.app.api.RemoteAppUserService;
import cn.cttic.app.mapper.BaseUserMapper;
import cn.cttic.common.core.enums.UserStatus;
import cn.cttic.common.core.exception.ServiceException;
import cn.cttic.common.core.exception.user.UserException;
import cn.cttic.common.core.utils.BeanCopyUtils;
import cn.cttic.common.satoken.model.AppLoginUser;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * 用户服务
 *
 * @author Times.Studio
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteAppUserServiceImpl implements RemoteAppUserService {

    private final BaseUserMapper baseUserMapper;

    @Override
    public AppLoginUser getUserInfo(String username) throws UserException {
        BaseUser baseUser = baseUserMapper.selectOne(new LambdaQueryWrapper<BaseUser>()
            .select(BaseUser::getUserName, BaseUser::getPhone, BaseUser::getPassword, BaseUser::getStatus)
            .eq(BaseUser::getUserName, username), false);
        if (ObjectUtil.isNull(baseUser)) {
            throw new UserException("user.not.exists", username);
        }
        if (UserStatus.DISABLE.getCode().equals(String.valueOf(baseUser.getStatus()))) {
            throw new UserException("user.blocked", username);
        }
        // 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可
        // 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
        return buildLoginUser(baseUserMapper.selectUserByUserName(username));
    }

    @Override
    public AppLoginUser getUserInfoByPhonenumber(String phonenumber) throws UserException {
        BaseUser sysUser = baseUserMapper.selectOne(new LambdaQueryWrapper<BaseUser>()
            .select(BaseUser::getPhone, BaseUser::getUserName, BaseUser::getPassword, BaseUser::getStatus)
            .eq(BaseUser::getPhone, phonenumber));
        if (ObjectUtil.isNull(sysUser)) {
            throw new UserException("user.not.exists", phonenumber);
        }
        if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
            throw new UserException("user.blocked", phonenumber);
        }
        // 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可
        // 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
        return buildLoginUser(baseUserMapper.selectUserByPhonenumber(phonenumber));
    }

    /**
     * 通用userId获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public AppLoginUser selectById(String userId) {
        BaseUser BaseUser = baseUserMapper.selectById(userId);
        if (BaseUser == null) {
            throw new NullPointerException("用户不存在");
        }
        return buildLoginUser(BaseUser);
    }

    /**
     * 构建登录用户
     */
    private AppLoginUser buildLoginUser(BaseUser user) {
        AppLoginUser loginUser =
        BeanCopyUtils.copy(user, AppLoginUser.class);
        loginUser.setUserId(user.getId());
        return loginUser;
    }

}
